AC_INIT(autogen.sh)

VERSION="0.2.0"
LT_VERSION="0:2:0"

UCOMMON_REQUIRES="6.6.0"

AC_CONFIG_AUX_DIR(autoconf)
AC_CANONICAL_SYSTEM
AC_PROG_CPP
AC_PROG_CC
AC_PROG_CXXCPP
AC_PROG_CXX
AC_LIBTOOL_WIN32_DLL
AM_PROG_LIBTOOL
AM_INIT_AUTOMAKE(apennine, [$VERSION])
AM_CONFIG_HEADER(apennine-config.h)

AC_C_RESTRICT
AC_C_VOLATILE
AC_C_INLINE

AC_SUBST(LT_VERSION)

if test -z "$PKG_CONFIG_PATH" ; then
    case "$prefix" in
    NONE|/usr/local|/usr)
        PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:/usr/lib/pkgconfig"
        ;;
    *)
        PKG_CONFIG_PATH="$prefix/lib/pkgconfig"
        ;;
    esac
else
    case "$prefix" in
    NONE|/usr/local|/usr)
        PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig"
        ;;
    *)
        PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$prefix/lib/pkgconfig"
        ;;
    esac
fi

export PKG_CONFIG_PATH

AC_ARG_ENABLE(debug,
    AC_HELP_STRING([--enable-debug],[compile for debugging]))
if test -z "$enable_debug" ; then
    enable_debug="no"
elif test $enable_debug = "yes" ; then
    CXXFLAGS="${CXXFLAGS} -g -DDEBUG"
fi

AC_ARG_WITH(pkg-config,
    AC_HELP_STRING([--with-pkg-config],[enable support for pkg-config]),[
    PKG_CHECK_MODULES(UCOMMON, ucommon >= $UCOMMON_REQUIRES)
],[
    AC_PATH_PROG([UCOMMON],[ucommon-config],[none])
    if test $UCOMMON = "none" ; then
        AC_ERROR("required ucommon library missing")
    fi
    UCOMMON_CFLAGS=`$UCOMMON --cflags`
    UCOMMON_LIBS=`$UCOMMON --libs`
])

AC_CHECK_FUNCS(mkfifo setpgrp setrlimit getuid execvp)
AC_CHECK_HEADERS(sys/resource.h syslog.h ioctl.h pwd.h)

AC_TYPE_SIGNAL
AC_CACHE_CHECK(whether sigwait has 2 arguments,
    ac_cv_libc_sigwait2,
    AC_TRY_COMPILE([
        #define _POSIX_PTHREAD_SEMANTICS
        #define _GNU_SOURCE
        #include <stdio.h>
        #include <signal.h>],
        [sigset_t sigs; int signo; sigwait(&sigs, &signo);],
        ac_cv_libc_sigwait2=yes,
        ac_cv_libc_sigwait2=no
    )
)
if test "$ac_cv_libc_sigwait2" = "yes" ; then
    AC_DEFINE(HAVE_SIGWAIT2, [1], [2 argument form])
    AC_DEFINE(HAVE_SIGWAIT, [1], [have sigwait])
else
    AC_CACHE_CHECK(whether has any sigwait support,
        ac_cv_libc_sigwait,
        AC_TRY_COMPILE([
            #define _POSIX_PTHREAD_SEMANTICS
            #define _GNU_SOURCE
            #include <stdio.h>
            #include <signal.h>],
            [sigset_t sigs; int signo = sigwait(&sigs);],
            ac_cv_libc_sigwait2=yes,
            ac_cv_libc_sigwait2=no
        )
    )

    if test "$ac_cv_libc_sigwait" = "yes" ; then
        AC_DEFINE(HAVE_SIGWAIT, [1], [have sigwait])
    else
        AC_CHECK_FUNCS(sigwait)
    fi
fi

AC_DEFUN([AC_SUBST_DIR], [
    ifelse($2,,,$1="[$]$2")
    result="***"
    prior="A"
    while test "$prior" != "$result" ; do
        prior=`(echo "[$]$1")`
        $1=`(
        test "x$prefix" = xNONE && prefix="$ac_default_prefix"
        test "x$exec_prefix" = xNONE && exec_prefix="${prefix}"
        eval echo \""[$]$1"\"
        )`
        result=`(echo "[$]$1")`
    done
    AC_SUBST($1)
])

test "$localstatedir" = '${prefix}/var' && localstatedir="/var"
test "$sysconfdir" = '${prefix}/etc' && sysconfdir="/etc"
test -z "$plugindir" && plugindir='${libdir}/apennine'
test -z "$cgibindir" && cgibindir='${libexecdir}/apennine'

AC_SUBST_DIR(default_cfgpath, sysconfdir)
AC_SUBST_DIR(default_varpath, localstatedir)
AC_SUBST_DIR(default_libpath, plugindir)
AC_SUBST_DIR(default_cgipath, cgibindir)
AC_SUBST_DIR(prefix_libdir, libdir)

AC_DEFINE_UNQUOTED(DEFAULT_CFGPATH, "$default_cfgpath", [config path])
AC_DEFINE_UNQUOTED(DEFAULT_VARPATH, "$default_varpath", [run path])
AC_DEFINE_UNQUOTED(DEFAULT_PLUGINS, "$default_libpath", [lib path])
AC_DEFINE_UNQUOTED(DEFAULT_LIBEXEC, "$default_cgipath", [script path])

AC_SUBST(cgibindir)
AC_SUBST(plugindir)
AC_SUBST(UCOMMON_CFLAGS)
AC_SUBST(UCOMMON_LIBS)
AC_SUBST(UCOMMON_REQUIRES)
AC_SUBST(CXXFLAGS)
AC_OUTPUT(Makefile inc/Makefile inc/apennine/Makefile
common/Makefile server/Makefile utils/Makefile)

